package com.app.TCES.controller;




import java.util.HashMap;
import java.util.List;
import java.util.Map;


import javax.servlet.http.HttpSession;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.app.TCES.domain.Indicators;
import com.app.TCES.domain.SelectFS;
import com.app.TCES.domain.Teacher;
import com.app.TCES.domain.TeacherSet;
import com.app.TCES.service.TeacherService;


@Controller
public class TeacherController {
    @Autowired
	TeacherService teacherService;
	//教师修改密码
	  @RequestMapping("/TchCGPW")
	  @ResponseBody
	  public Map<String, String> TchCGPW(Teacher teacher){
		  
		  teacherService.TchCGPW(teacher);
		  
		  Map<String, String> map = new HashMap<String, String>(); 
		  map.put("msg","成功");
		  return map;

	  }
	  
	  @RequestMapping("/insertTeacher")  //添加教师
      @ResponseBody
	  public Map<String, String> insertTeacher(TeacherSet teacherSet){
		  
		  Map<String, String> map = new HashMap<String, String>(); 
		  
			
		   int courseId = teacherService.getCouseId(teacherSet);
		   int deptId = teacherService.getDeptId(teacherSet);
		   
		   int x  = teacherService.selectTeacher(teacherSet);
           if(x > 0) {
        	   map.put("msg","教师号被占有");
        	   return map;
           }else {
        	   teacherService.insertTeacher(deptId,teacherSet);
    		   teacherService.insertTeacherCourse(courseId,teacherSet);
    		   map.put("msg","成功");
    		   return map;
           }
		
		   
		 

	  }
	  //删除教师
	  @RequestMapping("/deleteTeacher")
	  @ResponseBody
	  public Map<String, String> deleteTeacher(Integer tchId){
		  
		  teacherService.deleteTeacher(tchId);
		  
		  Map<String, String> map = new HashMap<String, String>(); 
		  map.put("msg","成功");
		  return map;

	  }
	  
	  //编辑教师
	  @RequestMapping("/updateTeacher")
	  @ResponseBody
	  public Map<String, String> updateTeacher(int tchId ,int courseId,int deptId){
		  
	

			  teacherService.updateTeacherDN(deptId,tchId);


			  teacherService.updateTeacherCN(courseId,tchId);

		  Map<String, String> map = new HashMap<String, String>(); 
		  map.put("msg","YES");
		  return map;

	  }
	  //重置密码
	  @RequestMapping("/TeacherRP")
	  @ResponseBody
	  public Map<String, String> TeacherRP(int tchId){
		  System.out.println(tchId);
		  teacherService.TeacherRP(tchId);
		  System.out.println("111");
		  Map<String, String> map = new HashMap<String, String>(); 
		  map.put("msg","YES");
		  return map;

	  }
	  
		@RequestMapping("/fuzzy_query") //模糊查询
		@ResponseBody
		public Map<String, Object> TeacherSet(String text,String deptName,@RequestParam("page")Integer page,@RequestParam("limit")Integer limit) {
			int tmp = (page-1)*limit;
			int tol;
	 	    	List<com.app.TCES.domain.TeacherSet> teacher;
			if(deptName.equals("全部院系")) {
				tol = teacherService.getto12(text);
				 teacher = teacherService.getTeacherFuzzyQ(text,tmp,limit);
			}else {
				tol = teacherService.getto13(text,deptName);
				 teacher = teacherService.getTeacherFuzzyQ1(text,deptName,tmp,limit);
			}
			
          
			 Map<String, Object> map = new HashMap<String, Object>();
			   map.put("tol",tol);
			   map.put("status",200);
			   map.put("massage","成功");
			   map.put("teacher",teacher);
			   
			return map;
		}
		//批量导入
		@SuppressWarnings("unused")
		@RequestMapping(value = "/import")
		public String exImport(@RequestParam(value = "filename")MultipartFile file, HttpSession session) {

			boolean a = false;
                   System.out.println("123");
			String fileName = file.getOriginalFilename();

			try {
				a = teacherService.batchImport(fileName, file);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return "redirect:Admin_teacher";
			
		}
		
		  //同行查询
		  @RequestMapping("/SelectTHPJ")
		  @ResponseBody
		  public  List<TeacherSet> SelectTHPJ(String tchName){
			  int tchId = teacherService.getTchId(tchName);
			  int deptId = teacherService.getDeptId2(tchName);
			  List<TeacherSet> teacherSet = teacherService.selectTeacher1(deptId,tchId);

			  return teacherSet;

		  }
		  //同行评分
		  @RequestMapping("/TonghangDaFen")
		  @ResponseBody
		  public Map<String, String>  TonghangDaFen(int score,String tchName1,String tchName){
			  Map<String, String> map= new HashMap<String, String>(); ;
			  int eva_teacher_id = teacherService.getTchId(tchName1);

			  int tchId = teacherService.getTchId(tchName);
			 Indicators i = teacherService.selectIndicators(eva_teacher_id,tchId);
			 if(i == null) {
				  System.out.println(tchId);
				  teacherService.TonghangDaFen(score,eva_teacher_id,tchId);
				  map.put("msg","成功");
			 }else {
				  map.put("msg","失败");
			 }
			  
			  return map;

		  }
		  //分数查询
		  @RequestMapping("/TeacherSelect")
		  public String SelectAllFenShu(Model model,HttpSession session){
			  String tchName = (String) session.getAttribute("userName");
			  
			  int tchId = teacherService.getTchId(tchName);
			  SelectFS selectFS = new SelectFS();
			  try {
				

	              int xsscore = teacherService.selectScore(tchId);
	              System.out.println("++++"+xsscore+"++++");
	              int thscore = teacherService.selectThScore(tchId);
	              System.out.println("++++"+thscore+"++++");
	              

	              selectFS.setTchId(tchId);
	              selectFS.setXsscore(xsscore);
	              selectFS.setThscore(thscore);
				
			} catch (Exception e) {
				
	              int xsscore = 0;
	              int thscore = 0;
	             
	              selectFS.setTchId(tchId);
	              selectFS.setXsscore(xsscore);
	              selectFS.setThscore(thscore);
				
			}
			  model.addAttribute("selectFS", selectFS);

			  return "/TeacherSelect";

		  }
		

		  
}
